package com.anche.cywebnew.Service.configManager.impl;

import cn.hutool.core.util.StrUtil;
import com.anche.cywebnew.DAO.configManager.ResourceMapper;
import com.anche.cywebnew.Pojo.systemManager.SysResource;
import com.anche.cywebnew.Service.configManager.ResourceService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;


    @Override
    public List<SysResource> getResourceTable() {
        return resourceMapper.getResourceAll();
    }

    @Override
    public void updateResource(SysResource sysResource) {
        resourceMapper.updateResource(sysResource);
    }

    @Override
    public void addResource(SysResource sysResource) {
        sysResource.setStatus(0);
        resourceMapper.insertResource(sysResource);
    }

    @Override
    public List<Object> getTreeResource(String regroup) {
        List<SysResource> resources = new ArrayList<>();
        if (StrUtil.isNotEmpty(regroup)){
            resources = resourceMapper.getResourceAllByRegroup(regroup);
        }else {
            resources = resourceMapper.getResourceAll();
        }
        List<Object> list = new ArrayList<>();
        for (SysResource t : resources) {
            if (t.getPid().equals("-1")) {//判断是否是一级菜单
                boolean flag;
                if (t.getStatus() == 0) {
                    flag = false;
                } else {
                    flag = true;
                }
                JSONObject treeObject = new JSONObject();
                if (t.getPid().equals("-1")) {
                    treeObject.put("spread", true);//是否直接展开
                }
                treeObject.put("id", t.getId());
                treeObject.put("title", t.getName());//tree的节点名称
                treeObject.put("disabled", flag);//layui中的是否禁用
                treeObject.put("children", getChildren(t.getId(), resources));//子节点，递归遍历
                list.add(treeObject);
            }
        }
        return list;
    }

    @Override
    public List<SysResource> getResourceByUrl(String url) {
        return resourceMapper.selectResourceByUrl(url);
    }

    @Override
    public void updateStatusById(String id, Integer status) {
        List<String> idsQuery = new ArrayList<>();
        List<String> idsResult = new ArrayList<>();
        idsQuery.add(id);
        idsResult.add(id);
        idsResult = getChildUseRecu(idsQuery, idsResult);
        resourceMapper.updateStatusByIds(idsResult, status);
    }

    @Override
    public void delResourceById(String id) {
        List<String> idsQuery = new ArrayList<>();
        List<String> idsResult = new ArrayList<>();
        idsQuery.add(id);
        idsResult.add(id);
        idsResult = getChildUseRecu(idsQuery, idsResult);
        resourceMapper.delResourceByIds(idsResult);
    }

    /**
     * 递归遍历所有层级的子集
     *
     * @param idsQuery  要查找的资源id的集合
     * @param idsResult 返回的所有层级的子集资源id的集合
     */
    private List<String> getChildUseRecu(List<String> idsQuery, List<String> idsResult) {
        List<String> childIds = resourceMapper.getIdsByPids(idsQuery);
        if (childIds.size() > 0) {
            for (String id : childIds) {
                idsResult.add(id);
            }
            getChildUseRecu(childIds, idsResult);
        }
        return idsResult;
    }

    public List<Object> getChildren(String parentId, List<SysResource> sysResources) {
        List<Object> list = new ArrayList<>();
        for (SysResource d : sysResources) {
            if (d.getPid().equals(parentId)) {
                boolean flag;
                if (d.getStatus() == 0) {
                    flag = false;
                } else {
                    flag = true;
                }
                JSONObject obj = new JSONObject();
                obj.put("id", d.getId());
                obj.put("title", d.getName());
                obj.put("disabled", flag);
                obj.put("children", getChildren(d.getId(), sysResources));
                list.add(obj);
            }
        }
        return list;
    }
}
